<?php

require('pagina.php');
require('database.php');
require('auth.php');
require('core.php');

// Verifica se esta autenticado com pelo menos nivel 3
validaPermissao(3);

// Inicia cabecalho da pagina
pagina_inicio('ParkSys - Employees');
adicionar_js('toggle.js');
adicionar_css('employees.css');
adicionar_css('datechooser.css');
adicionar_js('datechooser.js');
pagina_head();

// Mostra opcoes
echo "<ul id=opcoes>";
echo "<li class='positive'><a href='employee_new.php'><img src='img/add.png' alt=''>New Employee</a></li>";
echo "<li><a href='employees.php?" .
	(isset($_GET['from'])? "&amp;from={$_GET['from']}" : "") .
	(isset($_GET['to'])? "&amp;to={$_GET['to']}" : "") .
	(isset($_GET['search'])? "&amp;search={$_GET['search']}" : "") .
	(isset($_GET['location'])? "&amp;location={$_GET['location']}" : "") .
"&amp;print=1' target='_blank'><img src='img/print.png' alt=''>Print</a></li>";
echo "<li><a href='employees_table.php?" .
	(isset($_GET['from'])? "&amp;from={$_GET['from']}" : "") .
	(isset($_GET['to'])? "&amp;to={$_GET['to']}" : "") .
	(isset($_GET['search'])? "&amp;search={$_GET['search']}" : "") .
	(isset($_GET['location'])? "&amp;location={$_GET['location']}" : "") .
"' target='_blank'><img src='img/excel.png' alt=''>Excel Table</a></li>";
echo "</ul><br><br>";

// Pega o nome e id de todas as locacoes
$locations = get_all_locations();

// Formulario para entrar com o periodo de tempo
echo "<form id=periodo method=GET action=employees.php>";
echo "<label for=from>From:</label> <input id=from type=TEXT value='" . @$_GET['from'] . "' name='from' size=9>",
	"<img class=imgcalendar src='img/calendar.gif' onclick=\"showChooser(this, 'from', 'chooserSpan1', 2000, 2020, Date.patterns.ShortDatePattern, false);\" alt='calendar'>",
	"<div id='chooserSpan1' class='dateChooser select-free' style='display: none; visibility: hidden; width: 160px;'></div>";
echo "<label for=to>To:</label> <input id=to type=TEXT value='" . @$_GET['to'] . "' name='to' size=9>",
	"<img class=imgcalendar src='img/calendar.gif' onclick=\"showChooser(this, 'to', 'chooserSpan2', 2000, 2020, Date.patterns.ShortDatePattern, false);\" alt='calendar'>",
	"<div id='chooserSpan2' class='dateChooser select-free' style='display: none; visibility: hidden; width: 160px;'></div><br>";
echo "<label for=search>Search:</label> <input type=TEXT name=search id=search value='' maxlength=50 size=10> ";

echo "<br><label for=location>Subjects to location:</label> <select id=location name='location' >";
echo "<option value=0 >----any----</option>";
foreach ($locations as $loc)
{
	echo	"<option value={$loc['id']} ";
	if (@$_GET['location']==$loc['id'])
	{
		echo "selected=SELECTED";
		$location_name = $loc['name'];
	}
	echo ">{$loc['name']}</option>";
}
unset($locations);echo "</select><br>";

echo "<input type=SUBMIT value='submit' name='ok'>";
echo "</form>";

/* Show employees' table */
flush();
include('employees_table.php');

if (!isset($_GET['print']))
{
	$resultado = query_bd("SELECT * FROM employees WHERE NOT ISNULL(leaved)");

	echo "<em><a href=\"javascript:toggle('fired')\" >Not active employees(", mysql_affected_rows(), ")</a></em>";

	// Mostra uma tabela com todos os funcionarios despedidos
	echo "<table id=fired style='display: none'><tr>";
	echo "<th>Name<th>Contact<th>Leaved company";

	$mark = False;
	while ($employee = mysql_fetch_array($resultado))
	{
		$employee_contact = query_fetch("SELECT * FROM contacts WHERE id={$employee['contact_id']}");

		echo "<tr ", ($mark = !$mark? "class=mark" : ""), ">";
		echo "<td><a href='employee.php?id={$employee['id']}'>{$employee_contact['name']}</a>";
		echo "<td>{$employee_contact['number']} {$employee_contact['street']} {$employee_contact['complement']}<br>",
			"{$employee_contact['city']} {$employee_contact['state']} {$employee_contact['zipcode']}<br>",
			"{$employee_contact['phone']} {$employee_contact['cellphone']}<br>{$employee_contact['email']}";
		echo "<td>", date("M j Y", strtotime($employee['leaved']));

		unset($employee);
	}
	unset($resultado);

	echo "</table>";
}

// Finaliza pagina
pagina_fim();
?>
